//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.IO;
using System;
using System.Text.RegularExpressions;
using System.Drawing;

namespace Microsoft.TridentWordAddIn.Common
{
    public sealed class FileHelper
    {
        private FileHelper() { }

        /// <summary>
        /// Tries the get extension.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="extension">The extension.</param>
        public static string TryGetExtension(string value)
        {
            string extension = string.Empty;
            string[] results = value.Split(new char[] { '/' });

            if (results != null && results.Length == 2)
            {
                if (results[1].CompareIgnoreCase("plain"))
                {
                    extension = ".txt";
                }
                else
                {
                    extension = "." + results[1];
                }
            }

            if (value.IsFileExtension())
            {
                extension = value;
            }

            return extension;
        }

        /// <summary>
        /// Determines whether [is image file] [the specified file path].
        /// </summary>
        /// <param name="extension">The extension.</param>
        /// <returns>
        /// 	<c>true</c> if [is image file] [the specified file path]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsImageFile(string extension)
        {
            string[] imageExtensions = new string[] { ".png", ".jpeg", ".bmp", ".gif", ".jpg", ".tif", ".thm" };
            return extension.MatchesAtLeastOne(imageExtensions);
        }

        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        public static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                try
                {
                    File.Delete(filePath);
                }
                catch (IOException)
                {
                }
            }
        }

        /// <summary>
        /// Creates the name of the temporary.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="extension">The extension (If extension is string.Empty then .tmp will be used</param>
        /// <returns>string</returns>
        public static string CreateTemporaryFileName(string filePath, string extension)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                if (extension.IsNullOrEmpty())
                {
                    extension = ".tmp";
                }

                filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                filePath = Path.Combine(filePath, @"Temp\TridentAddInTemp");
                DirectoryHelper.CreateDirectory(filePath);
                filePath = Path.Combine(filePath, Guid.NewGuid().ToString() + extension);
            }

            return filePath;
        }

        /// <summary>
        /// Gets the file icon.
        /// </summary>
        /// <returns></returns>
        public static Icon GetFileIcon(string extension)
        {
            string filePath = CreateTemporaryFileName(string.Empty, extension);
            File.Create(filePath);
            return Icon.ExtractAssociatedIcon(filePath);
        }
    }
}
